package org.ysu.shoeShopping.controller;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.ysu.shoeShopping.entity.Shoesizes;
import org.ysu.shoeShopping.entity.Sizes;
import org.ysu.shoeShopping.service.ShoesizesService;
import org.ysu.shoeShopping.service.SizesService;
import org.ysu.shoeShopping.tools.Result;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 鞋尺码(Sizes)表控制层
 *
 * @author makejava
 * @since 2025-09-13 17:11:30
 */
@RestController
@RequestMapping("sizes")
@CrossOrigin(origins = "*")
public class SizesController {
    /**
     * 服务对象
     */
    @Resource
    private SizesService sizesService;

    @Resource
    private ShoesizesService shoesizesService;

    /**
     * 分页查询
     *
     * @param sizes 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @GetMapping
    public ResponseEntity<Page<Sizes>> queryByPage(Sizes sizes, PageRequest pageRequest) {
        return ResponseEntity.ok(this.sizesService.queryByPage(sizes, pageRequest));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public ResponseEntity<Sizes> queryById(@PathVariable("id") Integer id) {
        return ResponseEntity.ok(this.sizesService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param sizes 实体
     * @return 新增结果
     */
    @PostMapping
    public ResponseEntity<Sizes> add(Sizes sizes) {
        return ResponseEntity.ok(this.sizesService.insert(sizes));
    }

    /**
     * 编辑数据
     *
     * @param sizes 实体
     * @return 编辑结果
     */
    @PutMapping
    public ResponseEntity<Sizes> edit(Sizes sizes) {
        return ResponseEntity.ok(this.sizesService.update(sizes));
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping
    public ResponseEntity<Boolean> deleteById(Integer id) {
        return ResponseEntity.ok(this.sizesService.deleteById(id));
    }

    /**
     * 根据鞋子ID获取可选尺码以及剩余数量
     * 前端请求路径: /api/sizes/available/{productId}
     *
     * @param productId 鞋子商品ID
     * @return 可选尺码及剩余数量列表
     */
    @GetMapping("/available/{productId}")
    public Result<List<Map<String, Object>>> getAvailableSizes(@PathVariable("productId") Integer productId) {
        try {
            // 参数校验
            if (productId == null || productId <= 0) {
                return Result.fail("400", "商品ID无效");
            }

            // 根据鞋子ID查询所有相关的尺码库存信息
            List<Shoesizes> shoeSizesList = shoesizesService.findByShoeId(productId);

            List<Map<String, Object>> availableSizes = new ArrayList<>();

            // 防止空指针异常
            if (shoeSizesList != null && !shoeSizesList.isEmpty()) {
                for (Shoesizes shoeSizes : shoeSizesList) {
                    // 确保shoeSize对象不为空且有有效的sizeId
                    if (shoeSizes != null && shoeSizes.getSizeId() != null) {
                        // 查询尺码详细信息
                        Sizes sizeInfo = sizesService.queryById(shoeSizes.getSizeId());

                        if (sizeInfo != null && sizeInfo.getSizenum() != null) {
                            Map<String, Object> sizeData = new HashMap<>();
                            sizeData.put("sizeId", sizeInfo.getSizeid());
                            // sizenum是float类型，需要转换为字符串显示
                            //根据鞋id获取可选尺码以及剩余数量
                            sizeData.put("sizeName", String.format("%.1f", sizeInfo.getSizenum()));
                            sizeData.put("remainingStock", shoeSizes.getScount() != null ? shoeSizes.getScount() : 0);
                            sizeData.put("available", (shoeSizes.getScount() != null && shoeSizes.getScount() > 0));

                            availableSizes.add(sizeData);
                        }
                    }
                }
            }

            return Result.success(availableSizes);
        } catch (Exception e) {
            e.printStackTrace(); // 添加日志输出以便调试
            return Result.fail("500", "查询可选尺码失败: " + e.getMessage());
        }
    }

}

